from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from controllers.errors.character_errors import HTTPCharacterNotExistError
from controllers.errors.emoji_errors import HTTPEmojiPackNotExistError
from controllers.errors.robots_error import HTTPRobotNotActivatedError, HTTPRobotNotFoundError, HTTPRobotConfigError
from controllers.errors.timbre_errors import HTTPTimbreNotExistError
from libs.login import login_required
from fields.character_fields_def import character_fields
from fields.timbre_fields_def import base_timbre_fields
from fields.emoji_pack_fields_def import base_emoji_pack_fields
from fields.personal_fields_def import rob_account_extra_info_fields
from services.errors.character import CharacterNotExistError
from services.errors.emoji import EmojiPackageNotExistError
from services.errors.robots import RobotRemoteConfigError
from services.errors.robots import RobotNotActiveError, RobotNotExistError, RobotRemoteConfigError
from services.errors.timbre import TimbreNotExistError, TimbreNotSupportLanguageError
from services.robot.robot_remote_service import RobotRemoteService, TaskState
from services.robot.robots_service import RobotsService
from controllers.mini_app import api
from flask_babel import _

class RobotCharactersApi(Resource):

    @login_required
    @marshal_with(character_fields)
    def get(self,robot_id:str):
        characters = RobotsService.get_characters_by_robot(robot_id)
        return characters
    
class RobotTimbresApi(Resource):

    @login_required
    @marshal_with(base_timbre_fields)
    def get(self,robot_id:str):
        timbres = RobotsService.get_timbres_by_robot(robot_id)
        return timbres
    

class RobotEmojiPacksApi(Resource):
    @login_required
    @marshal_with(base_emoji_pack_fields)
    def get(self,robot_id:str):
        emoji_packs = RobotsService.get_emoji_packs_by_robot(robot_id)
        return emoji_packs
    
class RobotConfigApi(Resource):
    @login_required
    @marshal_with(rob_account_extra_info_fields)
    def post(self,robot_id:str):
        parser = reqparse.RequestParser()
        parser.add_argument('character_id', type=str, required=False,default=None,location='json')
        parser.add_argument('timbre_id', type=str, required=False,default=None,location='json')
        parser.add_argument('emoji_pack_id', type=str, required=False,default=None,location='json')
        parser.add_argument('postal_code', type=str, required=False,default=None,location='json')
        parser.add_argument('user_name', type=str, required=False,default=None,location='json')
        parser.add_argument('robot_name', type=str, required=False,default=None,location='json')
        parser.add_argument('chat_language', type=str, required=False,default="zh",location='json')
        parser.add_argument('volume',type=int,required=False,default=None,location='json')
        parser.add_argument('brightness',type=int,required=False,default=None,location='json')
        args = parser.parse_args()
        try:
            result = RobotsService.update_robot_config(current_user,robot_id,args)
            return result
        except TimbreNotSupportLanguageError as e:
            raise HTTPRobotConfigError(_("Timbre {} not support language {}").format(e.timbre_name,e.language))
        except RobotRemoteConfigError as e:
            raise HTTPRobotConfigError(e.description)
        except RobotNotExistError as e:
            raise HTTPRobotNotFoundError(e.robot_id)
        except RobotNotActiveError:
            raise HTTPRobotNotActivatedError()
        except CharacterNotExistError as e:
            raise HTTPCharacterNotExistError(e.character_id)
        except TimbreNotExistError as e:
            raise HTTPTimbreNotExistError(timbre_id=e.timbre_id)
        except EmojiPackageNotExistError as e:
            raise HTTPEmojiPackNotExistError(e.emoji_pack_id)
        except Exception as e:
            raise HTTPRobotConfigError(str(e))
        

class RobotTaskStateApi(Resource):
    @login_required
    def get(self,task_id:str):
        result = RobotRemoteService.task_state(task_id)
        if result:
            success,state,message = result
            if success:
                return {
                    "code":200,
                    "state":state.value,
                    "message":message
                },200
            else:
                return {
                    "code":400,
                    "message":message
                },200
        return {"code":400,"message":"get state failed"},200


# class Config
api.add_resource(RobotCharactersApi, '/robots/<string:robot_id>/characters')
api.add_resource(RobotTimbresApi, '/robots/<string:robot_id>/timbres')
api.add_resource(RobotEmojiPacksApi, '/robots/<string:robot_id>/emoji_packs')
api.add_resource(RobotConfigApi, '/robots/<string:robot_id>/config')
api.add_resource(RobotTaskStateApi, '/robots/task/<string:task_id>/state')